package com.autumn.zero.workflow.entitys;

import javax.persistence.Column;
import javax.validation.constraints.NotNull;

import org.apache.ibatis.type.JdbcType;
import org.hibernate.validator.constraints.Length;

import com.autumn.domain.entities.IStatusEntity;
import com.autumn.domain.entities.auditing.AbstractDefaultModifiedGmtEntity;
import com.autumn.mybatis.mapper.annotation.ColumnOrder;
import com.autumn.mybatis.mapper.annotation.ColumnType;
import com.autumn.mybatis.mapper.annotation.Index;
import com.autumn.util.ExceptionUtils;
import com.autumn.validation.annotation.NotNullOrBlank;
import com.autumn.zero.workflow.constants.ProcessDesignStatusConstants;

/**
 * 流程设计抽象
 * 
 * @author 杨昌国 2018-12-21 12:19:11
 */
public abstract class AbstractWorkflowDefinition extends AbstractDefaultModifiedGmtEntity
		implements IStatusEntity<Long> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8023037991508124675L;

	/**
	 * 最大流程名称长度
	 */
	public static final int MAX_FLOW_NAME_LENGTH = 50;

	/**
	 * 最大流程id长度
	 */
	public static final int MAX_PROCESS_ID_LENGTH = 100;

	/**
	 * 字段 formType
	 */
	public static final String FILED_FORM_TYPE = "formType";

	/**
	 * 字段 flowName
	 */
	public static final String FILED_FLOW_NAME = "flowName";

	/**
	 * 字段 processContent
	 */
	public static final String FILED_PROCESS_CONTENT = "processContent";

	/**
	 * 表单类型
	 */
	@NotNull(message = "表单类型不能为空")
	@Column(name = "form_type", nullable = false)
	@ColumnOrder(1)
	@Index
	private Integer formType;

	/**
	 * 流程名称
	 */
	@NotNullOrBlank(message = "流程名称不能为空")
	@Length(max = MAX_FLOW_NAME_LENGTH, message = "流程名称长度不能超过" + MAX_FLOW_NAME_LENGTH + "个字。")
	@Column(name = "form_name", nullable = false, length = MAX_FLOW_NAME_LENGTH)
	@ColumnType(jdbcType = JdbcType.VARCHAR)
	@ColumnOrder(2)
	@Index
	private String flowName;

	/**
	 * 流程id(无需客户端输入)
	 */
	@Length(max = MAX_PROCESS_ID_LENGTH, message = "流程id长度不能超过" + MAX_PROCESS_ID_LENGTH + "个字。")
	@Column(name = "process_id", nullable = false, length = MAX_PROCESS_ID_LENGTH)
	@ColumnType(jdbcType = JdbcType.VARCHAR)
	@ColumnOrder(2)
	@Index
	private String processId;

	/**
	 * 进程内容
	 */
	@Column(name = "process_content", nullable = false)
	@ColumnType(jdbcType = JdbcType.BLOB)
	@ColumnOrder(4)
	private String processContent;

	/**
	 * 
	 */
	@NotNull(message = "状态不能为空")
	@Column(name = "status", nullable = false)
	@ColumnOrder(20)
	private Integer status;

	/**
	 * 获取表单类型
	 * 
	 * @return
	 */
	public Integer getFormType() {
		return formType;
	}

	/**
	 * 设置表单类型
	 * 
	 * @param formType 表单类型
	 */
	public void setFormType(Integer formType) {
		this.formType = formType;
	}

	/**
	 * 获取流程名称
	 * 
	 * @return
	 */
	public String getFlowName() {
		return flowName;
	}

	/**
	 * 设置流程名称
	 * 
	 * @param flowName 流程名称
	 */
	public void setFlowName(String flowName) {
		this.flowName = flowName;
	}

	/**
	 * 获取流程id(无需客户端输入)
	 * 
	 * @return
	 */
	public String getProcessId() {
		return processId;
	}

	/**
	 * 设置流程id(无需客户端输入)
	 * 
	 * @param processId
	 */
	public void setProcessId(String processId) {
		this.processId = processId;
	}	

	/**
	 * 获取进程内容
	 * 
	 * @return
	 */
	public String getProcessContent() {
		return processContent;
	}

	/**
	 * 设置进程内容
	 * 
	 * @param processContent 进程内容
	 */
	public void setProcessContent(String processContent) {
		this.processContent = processContent;
	}

	@Override
	public Integer getStatus() {
		return status;
	}

	@Override
	public void setStatus(Integer status) {
		this.status = status;
	}

	@Override
	public void valid() {
		super.valid();
		if (!ProcessDesignStatusConstants.exist(this.getStatus())) {
			ExceptionUtils.throwValidationException("无效的流程状态。");
		}
	}
}
